const {
	Controller
} = require('uni-cloud-router')
module.exports = class AppController extends Controller {
	async init() {

	}

	/**
	 * 定时器，1：确定收货订单，佣金到账
	 */
	async timer() {
		console.log("定时器处理订单开始")
		await this.service.mall.order.timer();
		console.log("订单处理结束")
		//退款订单返回资金
		await this.refund()
	}


	/**
	 * 退款定时器
	 */
	async refund() {
		let models = ["mall"]
		console.log("开始退款任务")
		//所有模块都有可能退款
		for (let model of models) {
			let orderList = await this.service.order[model].getRefundOrders();
			if (orderList && orderList.length > 0) {
				for (let order of orderList) {
					let res = await this.service.payment[order.payInfo.type].refund({
						payInfo: order.payInfo,
						uid: order.uid,
						outTradeNo: order.payInfo.outTradeNo,
						outRefundNo: order.id,
						totalFee: order.payInfo.totalFee, //支付总金额
						refundFee: order.fee, //退款金额
						refundDesc: order.description, //退款原因
					});
					console.log("支付端口退款成功", order._id, res);
					//退款处理成功
					if (res !== false) {
						order.message = res;
						res = await this.service.order[model].refundSuccess(order);
						console.log("订单端口处理结果", res);
					}
				}
			} else {
				console.log("refund无需处理", model)
			}
		}
		console.log("结束退款任务")
	}
}
